package com.huike.web.controller.clues;

import java.sql.Time;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import org.bouncycastle.util.test.FixedSecureRandom;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.huike.clues.domain.TbActivity;
import com.huike.clues.service.ITbActivityService;
import com.huike.common.annotation.Log;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.enums.BusinessType;

import javax.xml.crypto.Data;

/**
 * 活动管理Controller
 *
 * @author wgl
 * @date 2021-04-01
 */
@RestController
@RequestMapping("/clues/activity")
public class TbActivityController extends BaseController {

    @Autowired
    private ITbActivityService tbActivityService;

    /**
     * 查询活动管理列表
     */
    @PreAuthorize("@ss.hasPermi('clues:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbActivity tbActivity) {
        startPage();
        List<TbActivity> list = tbActivityService.selectTbActivityList(tbActivity);
        Map<String, Object> countParms = tbActivityService.getCountByStatus();
        return getDataTable(list, countParms);
    }


    /**
     * 获取渠道下活动
     *
     * @param channel
     * @return
     */
    @GetMapping("/listselect/{channel}")
    public AjaxResult list(@PathVariable("channel") String channel) {
        //创建活动管理类对象
        TbActivity tbActivity = new TbActivity();
        //设置活动渠道来源是线上的还是线下的推广
        tbActivity.setChannel(channel);
        //将状态值设置为2
        tbActivity.setStatus("2");
        //创建一个泛型为活动管理对象的数组，用于添加筛选完后条件数组
        ArrayList<TbActivity> newTbActivities = new ArrayList<>();
        //获取当前时间将其转换成毫秒数，用于后续进行判断
        Long currentTime = new Date().getTime();
        //在xml文件中使用sql语句查询出筛选完后的条件的数组
        List<TbActivity> tbActivities = tbActivityService.selectTbActivityList(tbActivity);
        //遍历数组
        for (TbActivity activity : tbActivities) {
            //获取筛选完条件后的开始活动时间将其转换成毫秒值
            long beginTime = activity.getBeginTime().getTime();
            //获取筛选完条件后的结束活动时间将其转换成毫秒值
            long endTime = activity.getEndTime().getTime();
            //进行判断筛选出在开始活动和结束活动时间中正常运行的活动项目
            if (currentTime >= beginTime && currentTime <= endTime) {
                //将项目存入新的数组中
                newTbActivities.add(activity);
            }
        }
        return AjaxResult.success(newTbActivities);
    }

    /**
     * 获取活动管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('clues:activity:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(tbActivityService.selectTbActivityById(id));
    }

    /**
     * 新增活动管理
     */
    @PreAuthorize("@ss.hasPermi('clues:activity:add')")
    @Log(title = "活动管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbActivity tbActivity) {
        return toAjax(tbActivityService.insertTbActivity(tbActivity));
    }


    /**
     * 修改活动管理
     */
    @PreAuthorize("@ss.hasPermi('clues:activity:edit')")
    @Log(title = "活动管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbActivity tbActivity) {
        return toAjax(tbActivityService.updateTbActivity(tbActivity));
    }

    /**
     * 删除活动管理
     */
    @PreAuthorize("@ss.hasPermi('clues:activity:remove')")
    @Log(title = "活动管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tbActivityService.deleteTbActivityByIds(ids));
    }
}
